package com.digitalchina.lxnsy.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

/**
 * Success is the sum of small efforts, repeated day in and day out.
 * 成功就是日复一日那一点点小小努力的积累。
 * AndroidGroup：158423375
 * Author：Johnny
 * AuthorQQ：956595454
 * AuthorWX：Qiang_it
 * AuthorPhone：nothing
 * Created by 2016/12/1.
 */
public class DBhelper {
    private SQLiteDatabase db;
    private Context context;
    private DBManager dbm;

    public DBhelper(Context context) {
        super();
        this.context = context;
        dbm = new DBManager(context);
    }


    /**
     * 获取省
     *
     * @return
     */
    public ArrayList<Level> getProvince() {
        dbm.openDatabase();
        db = dbm.getDatabase();
        ArrayList<Level> list = new ArrayList<>();
        try {
            String sql = "select * from Province";
            Cursor cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            while (!cursor.isLast()) {
                String code = cursor.getString(cursor.getColumnIndex("province_code"));
                String name = cursor.getString(cursor.getColumnIndex("province_name"));
                Level level = new Level();
                level.setName(name);
                level.setCode(code);
                list.add(level);
                cursor.moveToNext();
            }
            String code = cursor.getString(cursor.getColumnIndex("province_code"));
            String name = cursor.getString(cursor.getColumnIndex("province_name"));
            Level level = new Level();
            level.setName(name);
            level.setCode(code);
            list.add(level);
            cursor.close();
        } catch (Exception e) {
            return null;
        }
        dbm.closeDatabase();
        db.close();
        return list;
    }

    /**
     * 获取市
     *
     * @param provinceCode
     * @return
     */
    public ArrayList<Level> getCity(String provinceCode) {
        dbm.openDatabase();
        db = dbm.getDatabase();
        ArrayList<Level> list = new ArrayList<>();
        try {
            String sql = "select * from City where province_code='" + provinceCode + "'";
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                while (!cursor.isLast()) {
                    String code = cursor.getString(cursor.getColumnIndex("city_code"));
                    String name = cursor.getString(cursor.getColumnIndex("city_name"));
                    Level level = new Level();
                    level.setName(name);
                    level.setCode(code);
                    list.add(level);
                    cursor.moveToNext();
                }
                String code = cursor.getString(cursor.getColumnIndex("city_code"));
                String name = cursor.getString(cursor.getColumnIndex("city_name"));
                Level level = new Level();
                level.setName(name);
                level.setCode(code);
                list.add(level);
            }
            cursor.close();
        } catch (Exception e) {
            return null;
        }
        dbm.closeDatabase();
        db.close();
        return list;
    }


    /**
     * 获取区
     *
     * @param cityCode
     * @return
     */
    public ArrayList<Level> getCounty(String cityCode) {
        dbm.openDatabase();
        db = dbm.getDatabase();
        ArrayList<Level> list = new ArrayList<>();
        try {
            String sql = "select * from County where city_code='" + cityCode + "'";
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                while (!cursor.isLast()) {
                    String code = cursor.getString(cursor.getColumnIndex("county_code"));
                    String name = cursor.getString(cursor.getColumnIndex("county_name"));
                    Level level = new Level();
                    level.setName(name);
                    level.setCode(code);
                    list.add(level);
                    cursor.moveToNext();
                }
                String code = cursor.getString(cursor.getColumnIndex("county_code"));
                String name = cursor.getString(cursor.getColumnIndex("county_name"));
                Level level = new Level();
                level.setName(name);
                level.setCode(code);
                list.add(level);
            }
            cursor.close();
        } catch (Exception e) {
            return null;
        }
        dbm.closeDatabase();
        db.close();
        return list;
    }


}
